﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using JN.Utility;

namespace Power.Config
{
	public class UserReference
	{
        private static readonly object _lockObj = new object();
        private static UserReference _instance = null;
        public static UserReference Instance
        {
            get
            {
                lock (_lockObj)
                {
                    if (_instance == null)
                    {
                        _instance = new UserReference();
                    }
                    return _instance;
                }
            }
        }
        public UserReference()
        {
        }
        #region property
        private string _user_id;
        private string _station_id;

        public string User_id
        {
            get { return _user_id; }
            set { _user_id = value; }
        }
        public string Station_id
        {
            get { return _station_id; }
            set { _station_id = value; }
        }

        #endregion

        #region method
        public bool LoadConfig(string file)
        {
            try
            {
                UserReference sysConfig;

                XmlSerializer serial = new XmlSerializer(typeof(UserReference));
                if (File.Exists(file))
                {
                    FileStream reader = new FileStream(file, FileMode.Open);
                    // Call the Deserialize method and cast to the object type.
                    sysConfig = (UserReference)serial.Deserialize(reader);
                    reader.Close();
                }
                else
                {
                    sysConfig = UserReference.Instance;
                }
                UserReference systemConfig = UserReference.Instance;
                systemConfig.User_id = sysConfig.User_id;
                systemConfig.Station_id = sysConfig.Station_id;

                return true;
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                return false;
            }
        }
        #endregion
    }
}
